﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2009-03-29 23:57:55Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections.Generic;
using System.Reflection;

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace Outcoder.InversionOfControl.Unity
{
	public static class UnityExtensions
	{
		static UnityServiceLocator unityServiceLocator;
		internal static bool Initialized { get; set; }

		[Obsolete("Use parameterless overload instead.")]
		public static void InitializeServiceLocator(this ServiceLocatorSingleton adapter, IUnityContainer container)
		{
			ArgumentValidator.AssertNotNull(container, "container");
			unityServiceLocator = new UnityServiceLocator(container);
			container.RegisterInstance<IUnityContainer>(container);
			container.RegisterInstance<IDependencyRegistrar>(new UnityDependencyRegistrar());
			ServiceLocator.SetLocatorProvider(() => unityServiceLocator);
			Initialized = true;
			Dependency.Initialized = true;
		}

		public static void InitializeServiceLocator(this IUnityContainer container)
		{
			ArgumentValidator.AssertNotNull(container, "container");
			unityServiceLocator = new UnityServiceLocator(container);
			container.RegisterInstance<IUnityContainer>(container);
			container.RegisterInstance<IDependencyRegistrar>(new UnityDependencyRegistrar());
			ServiceLocator.SetLocatorProvider(() => unityServiceLocator);
			Initialized = true;
			Dependency.Initialized = true;
		}

		[Obsolete]
		public static bool IsInitialized(this ServiceLocatorSingleton adapter)
		{
			/* We can improve this by associating UnityServiceLocators and initialized values with each container. 
			 * For now, this will do. If you need this to be done, let me know. [DV] */
			return Initialized;
		}

		//		public static void RegisterInstanceIfMissing<TFrom>(this IUnityContainer container, TFrom instance)
		//		{
		//			if (!container.IsTypeRegistered(typeof(TFrom)))
		//			{
		//				container.RegisterInstance<TFrom>(instance);
		//			}
		//		}
		//
		//		public static void RegisterTypeIfMissing<TFrom, TTo>(this IUnityContainer container, bool registerAsSingleton) where TTo : TFrom
		//		{
		//			if (!container.IsTypeRegistered(typeof(TFrom)))
		//			{
		//				if (registerAsSingleton)
		//				{
		//					container.RegisterType(typeof(TFrom), typeof(TTo), new ContainerControlledLifetimeManager());
		//				}
		//				else
		//				{
		//					container.RegisterType<TFrom, TTo>();
		//				}
		//			}
		//		}

		//		public static void RegisterDefault<T>(this IServiceRegistrar container) where T : class
		//		{
		//			var types = Assembly.GetExecutingAssembly().GetTypes();
		//			RegisterDefault<T>(container, types, false);
		//		}
		//
		//		public static void RegisterDefaultSingleton<T>(this IServiceRegistrar container) where T : class
		//		{
		//			var types = Assembly.GetExecutingAssembly().GetTypes();
		//			RegisterDefault<T>(container, types, true);
		//		}
		//
		//		static void RegisterDefault<T>(IServiceRegistrar container, IEnumerable<Type> types, bool useSingleton)
		//		{
		//			foreach (Type type in types)
		//			{
		//				Type tType = typeof(T);
		//				if (!tType.IsAssignableFrom(type) || type.IsInterface)
		//				{
		//					continue;
		//				}
		//				string className = tType.Name.Substring(1);
		//				if (type.Name.StartsWith("Default") || type.Name == className)
		//				{
		//					if (useSingleton)
		//					{
		//						container.RegisterInstance(typeof(T), type);
		//					}
		//					else
		//					{
		//						container.RegisterType(typeof(T), type);
		//					}
		//				}
		//			}
		//		}
	}
}
